<template>
  <div class="background">
    <div class="login-box">
        <!-- 通过:rules="loginFormRules"来绑定输入内容的校验规则 -->
        <el-form :rules="loginFormRules" ref="loginForm" :model="loginForm" label-position="right" label-width="auto" show-message>
            <span class="login-title">注册</span>
            <div style="margin-top: 5px"></div>
            <el-form-item label="用户名" prop="username">
                <el-col :span="22">
                    <el-input type="text" v-model="loginForm.username"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-col :span="22">
                    <el-input type="password" v-model="loginForm.password"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="验证码" prop="validCode">
                <el-col :span="10">
                    <el-input type="validCode" v-model="loginForm.validCode"></el-input>
                </el-col>
                <el-col :span="2">
                    <p></p>
                </el-col>
                <el-col :span="10">
                   <valid-code></valid-code>
                </el-col>
            </el-form-item>
            <el-form-item style="width: 10%">
                <el-button type="primary" @click="loginSubmit('loginForm')" style="width: 14pc;">注册</el-button>
            </el-form-item>
        </el-form>
    </div>
  </div>   
</template>
<script>
import validCode from '../view/util/validCode'
import axios from 'axios'
    export default {
        data () {
            return {
                loginForm: {
                    username: '',
                    password: '',
                    validCode: ''
                },
                // 表单验证，需要在 el-form-item 元素中增加 prop 属性
                loginFormRules: {
                    username: [
                        {required: true, message: '账号不可为空', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '密码不可为空', trigger: 'blur'}
                    ],
                    validCode: [
                        {required: true, message: '验证码不可为空', trigger: 'blur'}
                    ]
                },
                imgSrc: require('../assets/timg.jpg')
            }
        },
        methods: {
            loginSubmit(formName) {
                // 为表单绑定验证功能
                let validCodes = this.loginForm.validCode == this.$store.state.validCode
                this.$refs[formName].validate((valid) => {
                    if (valid && validCodes) {
                        // 使用 vue-router 路由到指定页面，该方式称之为编程式导航
                        this.reg()
                    } else {
                        return false;
                    }
                });
            },
            reg() {
                let formdata = new FormData();
                formdata.append('username', this.loginForm.username);
                formdata.append('password', this.loginForm.password);
　　　　　　　　　axios({
                    method: 'post',
                    url: 'http://localhost:8000/public/regester',
                    data: formdata
                }).then(res => {
                    if (res.data) {
                        this.$message({
                            message: '恭喜你，注册成功',
                            type: 'success'
                        });
                        this.$router.push('/')
                    } else {
                        this.$message.error('此用户已被注册');
                    }
                })
            }
        },
        components: {
            validCode
        }
    }
</script>

<style scoped>
    .background {
        width: 100vw;
        padding: 0;
        margin: 0;
        height: 100vh;
        font-size: 16px;
        background-position: left top;
        background-color: #242645;
        color: #fff;
        font-family: "Source Sans Pro";
        position: relative;
        background-image: url('../assets/timg4.jpg');
        background-repeat: no-repeat;
        background-size: 100% 100%;
        overflow: hidden;
    }
    .login-box {
        border: 1px solid #DCDFE6;
        width: 23%;
        margin: 8% auto;
        padding: 3% 3% 1% 3%;
        border-radius: 5px;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        box-shadow: 0 0 25px palegreen;
        background-color: white;
    }
    .login-title {
        text-align: center;
        margin: 0 auto 40px auto;
        color: #66CD00;
        font-size: 30px;
        font-weight: bold;
    }
</style>
